# -*- coding: utf-8 -*-

"""
jiandan.py：文件名全部用 jpg 后缀保存，导致部分 GIF 文件拓展名不对
"""

import os
import filetype

CURRENT_DIR = os.path.dirname(os.path.abspath(__file__))
IMG_DIR = os.path.join(CURRENT_DIR, 'jiandan')

for base, dirs, files in os.walk(IMG_DIR):
    print base, dirs
    print '-' * 50
    for f in files:
        fullpath = os.path.join(base, f)
        name, ext = os.path.splitext(f)
        ext = ext.lower()
        real_ext = filetype.guess_extension(fullpath)
        if not real_ext:  # real_ext of metadata.json is None
            continue
        real_ext = '.' + real_ext
        if ext != real_ext:
            print '%-15s => %s' % (f, real_ext)
            newpath = os.path.join(base, name + real_ext)
            os.rename(fullpath, newpath)
